package com.xcalendar.database.dao;

import java.util.List;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.jdo.Transaction;

import com.google.appengine.api.datastore.Key;
import com.xcalendar.database.persistence.PMF;
import com.xcalendar.database.table.Event;
import com.xcalendar.database.table.Location;
import com.xcalendar.database.table.User;

public class LocationDAO {
	public void addLocation(Location location) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction txn = pm.currentTransaction();
		try {
			txn.begin();
			User user = pm.getObjectById(User.class,location.getUserKey());
			user.getLocations().add(location);
			pm.makePersistent(user);
			txn.commit();
		} finally {
			if (txn.isActive()) {
				txn.rollback();
			}
			pm.close();
		}
	}

	public Location readLocation(Key key) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Location location;
		try {
			location = pm.getObjectById(Location.class, key);
		} catch (JDOObjectNotFoundException e) {
			location = null;
		} finally {
			pm.close();
		}
		return location;
	}

	public void deleteLocation(Location location) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction txn = pm.currentTransaction();
		try {
			txn.begin();
			User user = pm.getObjectById(User.class,location.getUserKey());
			location = pm.getObjectById(Location.class, location.getKey());
			user.getLocations().remove(location);
			List<Event> returnedEventsList = user.getEvents();
			for (Event event : returnedEventsList) {
				if(event.getLocationKey()!=null)
					if(event.getLocationKey().equals(location.getKey()))
						event.setLocationKey(null);
			}
			pm.deletePersistent(location);
			pm.makePersistent(user);
			txn.commit();
		} finally {
			if (txn.isActive()) {
				txn.rollback();
			}
			pm.close();
		}
	}
	public void deleteLocation(Key locationKey) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction txn = pm.currentTransaction();
		try {
			txn.begin();
			Location location = pm.getObjectById(Location.class, locationKey);
			User user = pm.getObjectById(User.class,locationKey.getParent());
			user.getLocations().remove(location);
			List<Event> returnedEventsList = user.getEvents();
			for (Event event : returnedEventsList) {
				if(event.getLocationKey()!=null)
					if(event.getLocationKey().equals(location.getKey()))
						event.setLocationKey(null);
			}
			pm.deletePersistent(location);
			pm.makePersistent(user);
			txn.commit();
		} finally {
			if (txn.isActive()) {
				txn.rollback();
			}
			pm.close();
		}
	}

	public void updateLocation(Location location) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction txn = pm.currentTransaction();

		String locationName = location.getLocationName();
		Double longitude = location.getLongitude();
		Double latitude = location.getLatitude();

		try {
			txn.begin();
			location = pm.getObjectById(Location.class, location.getKey());
			location.setLocationName(locationName);
			location.setLongitude(longitude);
			location.setLatitude(latitude);
			pm.makePersistent(location);
			txn.commit();
		} finally {
			if (txn.isActive()) {
				txn.rollback();
			}
			pm.close();
		}
	}
}
